/**
 * 
 */
package com.banyingli.common;

import java.io.File;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.os.StatFs;
import android.util.Log;

/**
 * @ClassName: Util
 * @Description: TODO 各种通用的方法
 * @date 2011-8-10 下午05:20:13
 * 
 */
public class Util {
	/**
	 * 计划实现的方法 1.根据给定的参数返回AlertView 2.下载 3.保存文件到SD卡 4.从网络获取图片 5.歌曲的播放 6.检查网络
	 * 7.检查SD卡剩余空间
	 * 
	 * 
	 * */

	/**
	 * 检查SD卡剩余空间大小
	 * 
	 * */
	public static long getSDCardSize() {

		String state = Environment.getExternalStorageState();
		if (Environment.MEDIA_MOUNTED.equals(state)) {
			File path = Environment.getExternalStorageDirectory();
			StatFs stat = new StatFs(path.getPath());
			long blockSize = stat.getBlockSize();
			long availableBlocks = stat.getAvailableBlocks();
			return availableBlocks * blockSize;

		}
		return 0;
	}

	/** 判断WIFI是否可用 */
	public static boolean isWiFiActive(Context inContext) {
		Context context = inContext.getApplicationContext();
		ConnectivityManager connectivity = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		if (connectivity != null) {
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						if (info[i].getTypeName().equals("WIFI")) {
							return true;
						} else {
							return true;
						}
					}
				}
			}
		}
		return false;
	}

	/** 判断网络是否可用 */
	public static boolean isNetworkAvailable(Context ctx) {
		Context context = ctx;
		ConnectivityManager connectivity = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity == null) {
			// Error
			Log.e("netWork", "连接网络超时");
		} else {
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						Log.e("netWork", "网络可以使用");
						return true;
					}
				}
			}
		}
		return false;
	}

	/**
	 * 检查文件是否存在
	 * 
	 * */
	public static boolean fileIsExist(String path) {
		boolean fileIsExist = false;
		File zip = new File(path);
		if (zip.exists()) {
			fileIsExist = true;
			return fileIsExist;
		}

		return fileIsExist;
	}
}
